<?php

namespace app\index\controller;

use app\common\model\SiteConfig;
use app\common\model\Sort;
use app\common\model\Ad;
use app\common\model\Question;
use app\common\model\User;
use think\facade\Session;
use think\Request;
use think\facade\Cache;

class Index extends Base
{
    //首页
    public function index()
    {
        $sort = new Sort();
        $sort_list = $sort->getsortlist();
        $site_info = seo('',0);
        $question_list = CacheGet('index_list');
        if(!$question_list){
            $question_list = Question::limit(10)->order('Id desc')->with('profile')->select();
            CacheSet('index_list',$question_list);
        }
        $question_num = Question::count();
        $this->assign([
            'site_seo' => $site_info,
            'sort_list' => $sort_list,
            'question_list' => $question_list,
            'question_num' => $question_num
        ]);
        return $this->fetch('index/index');
    }
    //前台 - 分类聚合
    public function classify()
    {
        $sort = new Sort();
        //分类数据
        $sort_list = $sort->getsortlist();
        //seo数据
        $site_info = seo('',3);
        $this->assign([
            'site_seo' => $site_info,
            'sort_list' => $sort_list
        ]);

        return $this->fetch();
    }
    //前台 - 分类页
    public function cate()
    {
        $sorts = new Sort;
        $alias = $this->request->param('type');
        $alias_c = $this->request->param('c');
        if (isset($alias_c)) {
            $this->assign('sort_c', '');
            //二级目录
            $sort_info = $sorts->gets($alias_c);
            //更新seo
            $seo_data = [
                'two_name'=>$sort_info['name'],
                'key'=>$sort_info['key'],
                'desc'=>$sort_info['desc'],
                'one_name'=>$sorts->gets($alias)['name']
            ];
            $site_info  = seo($seo_data,2);
        } else {
            //分类seo渲染
            $sort_info = $sorts->gets($alias);
            $this->assign('sort_c', $sorts->gets_c($alias));
            //更新seo
            $seo_data = $sort_info;
            $site_info  = seo($seo_data,1);
        }
        if ($sort_info == false) {
            return $this->redirect('index/index/i_404');
        }

        $que = new Question;
        //试题数据
        $list = CacheGet('cate_question_list_'.$sort_info['Id'].'_'.$this->request->param('page'));
        if(!$list){
            $list = $que->get_list($sort_info['Id'], 15);
            CacheSet('cate_question_list_'.$sort_info['Id'].'_'.$this->request->param('page'),$list);
        }
        $page = $list->render();
        if (count($list) < 1) {
            $this->assign([
                'no_data' => '1'
            ]);
        } else {
            $this->assign([
                'no_data' => '0'
            ]);
        }
        //最热推荐
        $hot_list = CacheGet('cate_hot_list');
        if(!$hot_list){
            $hot_list = $que->get_hotList($sort_info['Id']);
            CacheSet('cate_hot_list',$hot_list);
        }

        $this->assign([
            'site_seo' => $site_info,
            'sort_info' => $sort_info,
            'list' => $list,
            'hot_list' => $hot_list,
            'page' => $page
        ]);

        return $this->fetch();
    }
    //前台 - 试题页面
    public function ask()
    {
        $question = new Question();
        $id = $this->request->param('id/d');
        //查询指定试题信息
        $info = $question->getone($id);

        if ($info == false) {
            return $this->redirect('index/index/i_404');
        }
        $this->assign('soft_a', '');
        $sorts = new Sort;
        if ($info['profile']['fid'] != 0) {
            $sort_a = $sorts->gets_id($info['profile']['fid']);
            $this->assign('sort_a', $sort_a);
        }

        //增加浏览量
        $question->look($id);
        //最热推荐
        $hot_list = CacheGet('cate_hot_list');
        if(!$hot_list){
            $hot_list = $question->get_hotList($info['type']);
            CacheSet('cate_hot_list',$hot_list);
        }

        $seo_data = [
            'title'=>$info['title'],
            'option'=>$info['option']
        ];
        $site_info = seo($seo_data,4);

        $this->assign([
            'site_seo' => $site_info,
            'question_info' => $info,
            'hot_list' => $hot_list
        ]);
        return $this->fetch();
    }
    //前台 - 登陆页
    public function login()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证码
            if (!captcha_check($data['verify'])) {
                return json_x(-1, '验证码输入错误！');
            }
            // 验证数据
            $rule = [
                'username|用户名' => 'require|alphaDash|length:3,20',
                'password|密码' => 'require|length:3,20',
            ];
            $result = $this->validate($data, $rule);
            if (true !== $result) {
                return json_x(-1, $result);
            }

            $u = new User();
            if ($u->login($data['username'], $data['password'])) {
                return json_x(0, '登陆成功');
            } else {
                return json_x(-1, '登陆失败(请检查账号密码是否正确)');
            }
        } else {
            $site_info = seo('登陆账号', 5);
            $this->assign([
                'site_seo' => $site_info
            ]);
            return $this->fetch();
        }
    }
    //发送短信验证码
    public function smscode(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证码
            if (!captcha_check($data['verify'])) {
                return json_x(-1, '验证码输入错误！');
            }
            // 验证数据
            $rule = [
                'username|手机号码' => 'require|alphaDash|length:11',
            ];
            $result = $this->validate($data, $rule);
            if (true !== $result) {
                return json_x(-1, $result);
            }
            $u = new User();
            $result = $u->sendAliDaYuAuthCode($data['username']);
            if ($result === true) {
                return json_x(0, '发送成功');
            } else {
                return json_x(-1, $result);
            }
        } else {
            return json_x(-1,'失败');
        }
    }
    //前台 - 注册页
    public function reg()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
//            //验证码
//            if (!captcha_check($data['verify'])) {
//                return json_x(-1, '验证码输入错误！');
//            }
            // 验证数据
            $rule = [
                'username|手机号码' => 'require|alphaDash|length:11',
                'password|密码' => 'require|length:3,20',
            ];
            $result = $this->validate($data, $rule);
            if (true !== $result) {
                return json_x(-1, $result);
            }
            $u = new User();
            $result = $u->reg($data);
            if ($result === true) {
                return json_x(0, '注册成功');
            } else {
                return json_x(-1, $result);
            }
        } else {
            $invite = $this->request->param('invite');
            if(isset($invite)){
                //有邀请链接，写入session
                Session::set('invite',$invite);
            }

            $site_info =  seo('注册账号', 5);
            $this->assign([
                'site_seo' => $site_info
            ]);
            return $this->fetch();
        }
    }
    //前台 退出登陆
    public function out()
    {
        Session::delete('main_user');
        return $this->redirect('index/index/index');
    }
    //404
    public function i_404()
    {
        return $this->fetch();
    }
}
